﻿using System;
using System.Collections.Generic;
using System.Web;
using SubSonic;

namespace AppBox
{
    public class XConfigHelper
    {
        #region fields & constructor

        /// <summary>
        /// 缓存在内存
        /// </summary>
        private static XConfigCollection configs = new XConfigCollection();

        /// <summary>
        /// 载入所有的配置项
        /// </summary>
        static XConfigHelper()
        {
            ReloadColl();
        }

        /// <summary>
        /// 重新加载所有的配置项
        /// </summary>
        public static void ReloadColl()
        {
            configs = new Select().From<XConfig>()
                .ExecuteAsCollection<XConfigCollection>();
        }
        #endregion

        #region methods
        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            foreach (XConfig config in configs)
            {
                if (config.ConfigKey == key)
                {
                    return config.ConfigValue;
                }
            }

            return String.Empty;
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetValue(string key, string value)
        {
            foreach (XConfig config in configs)
            {
                if (config.ConfigKey == key)
                {
                    config.ConfigValue = value;
                }
            }
        }

        /// <summary>
        /// 保存所有更改的配置项
        /// </summary>
        public static void SaveAll()
        {
            configs.SaveAll();
        }
        #endregion

        #region properties

        /// <summary>
        /// 网站标题
        /// </summary>
        public static string Title
        {
            get
            {
                return GetValue("Title");
            }
            set
            {
                SetValue("Title", value);
            }
        }

        /// <summary>
        /// 列表每页显示的个数
        /// </summary>
        public static int PageSize
        {
            get
            {
                return Convert.ToInt32(GetValue("PageSize"));
            }
            set
            {
                SetValue("PageSize", value.ToString());
            }
        }

        /// <summary>
        /// 帮助下拉列表
        /// </summary>
        public static string HelpList
        {
            get
            {
                return GetValue("HelpList");
            }
            set
            {
                SetValue("HelpList", value);
            }
        }



        #endregion
    }
}
